/**
 * 
 */

package com.tools.cleanmaster.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.tools.cleanmaster.R;

/**
 * @author ouyang
 */
public class CheckBoxThreeStates extends CheckBoxPreAction {

    private boolean mIsHalfChecked = false;
    private Drawable mHalfCheckDrawble;
    private Drawable mNormalCheckDrawble;
    private Drawable mCurCheckDrawble = null;

    /**
     * @param context
     */
    public CheckBoxThreeStates(Context context) {
        super(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public CheckBoxThreeStates(Context context, AttributeSet attrs) {
        super(context, attrs);
        readAttrs(context, attrs);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CheckBoxThreeStates(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        readAttrs(context, attrs);
    }

    private void readAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CheckBoxThreeStates);
        int indexCount = a.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.CheckBoxThreeStates_halfCheckedDrawble) {
                mHalfCheckDrawble = a.getDrawable(attr);
            } else if (attr == R.styleable.CheckBoxThreeStates_normalCheckedDrawble) {
                mNormalCheckDrawble = a.getDrawable(attr);
            }
        }
        a.recycle();
    }

    /**
     * 设置是否为半选状态，注意，当为半选状态时，
     * @param isHalfChecked
     */
    public void setChecked(boolean isChecked, boolean isHalfChecked) {
        mIsHalfChecked = isHalfChecked;
        if (isHalfChecked) {
            if (mCurCheckDrawble != mHalfCheckDrawble) {
                setButtonDrawable(mHalfCheckDrawble);
            }
            if (isChecked()) {
                setChecked(false);
            }
        } else {
            if (mCurCheckDrawble != mNormalCheckDrawble) {
                setButtonDrawable(mNormalCheckDrawble);
            }
            setChecked(isChecked);
        }
    }

    @Override
    public void setChecked(boolean checked) {
        mIsHalfChecked = false;
        super.setChecked(checked);
    }
}
